<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class km_cabang extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('km_cabang_model', 'Cabang');
		$this->load->model('km_dt_cabang_model', 'Detail');
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Cabang";
		
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();
		
		viewByTemplate('km_cabang/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Cabang->GetJSON();
	}

		function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->Detail->GetJSON(post('x'));
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "Cabang Baru";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";		
		viewByTemplate('km_cabang/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Cabang";
		$data['data'] = $this->Cabang->GetSingleView(get('cabang_kode'));
		
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('km_cabang/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Cabang";
		$data['data'] = $this->Cabang->GetSingleView(get('cabang_kode'));
		
		$data['mode'] = "view";
		viewByTemplate('km_cabang/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		//cabang_id, cabang_kode, cabang_nama, cabang_npwp, cabang_nopkp, cabang_tglpkp, cabang_alamat, cabang_telp, cabang_fax, cabang_tipe
		if (post("cabang_kode") == '') {
			$error->Add("cabang_kode", "Kode Cabang", $tpl_empty);
		} else if (post("cabang_nama") == '') {
			$error->Add("cabang_nama", "Nama Cabang", $tpl_empty);
		} else if (post("cabang_alamat") == '') {
			$error->Add("cabang_alamat", "Alamat Cabang", $tpl_empty);
		} else if (post("cabang_telp") == '') {
			$error->Add("cabang_telp", "Telp Cabang", $tpl_empty);
		} else if (post("cabang_fax") == '') {
			$error->Add("cabang_fax", "Fax Cabang", $tpl_empty);
		} else if (post("cabang_tipe") == '') {
			$error->Add("cabang_tipe", "Tipe Cabang", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM km_cabang WHERE cabang_kode = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("cabang_kode"))))."'")) > 0) {
			$error->Add("cabang_kode", "Kode Cabang", $tpl_duplicate);
		}		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('cabang_kode', 'cabang_nama', 'cabang_npwp', 'cabang_nopkp', 'cabang_tglpkp', 'cabang_alamat', 'cabang_telp', 'cabang_fax', 'cabang_tipe'));
				$data['cabang_nama']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['cabang_nama'])));
				$data['cabang_tipe']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['cabang_tipe'])));
				
				if (post("mode") == "create")
				{
					$this->Cabang->Add($data);
					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					$this->Cabang->Update($data, array('cabang_kode'	=> $data['cabang_kode']));
					$ls_response = "Data Tersimpan";
				}
			}


		// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];
				

				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->Detail->DeleteSingle($row['return_toko_details_id']);
				}
				
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					
					// Auto number kode detail return
					
					
					$query = $this->db->query("SELECT return_toko_details_id FROM (SELECT * FROM kst_return_toko_details2 ORDER BY return_toko_details_id DESC) WHERE rownum <= 1");
					
					if($query->num_rows() > 0){
						$getquery = $query->row_array();
						$rowcount = $getquery['return_toko_details_id'];
						$autonum = $rowcount + 1;
					} else {
						$autonum = 1;
					}

					$row = $dtl_sample2['inserted'][$i];
					$row['return_toko_details_id'] = $autonum;
					$row['return_toko_id'] = $return_toko_id_x;
					$row['return_toko_details_barang'] = $row['return_toko_details_barang'];
					$row['return_toko_details_barcode'] = $row['return_toko_details_barcode'];
					$row['return_toko_details_type'] = $row['return_toko_details_type'];
					$row['return_toko_details_deskripsi'] = $row['return_toko_details_deskripsi'];
					$row['return_toko_details_jumlah'] = $row['return_toko_details_jumlah'];
					$row['return_toko_details_hpokok'] = $row['return_toko_details_hpokok'];
					$row['return_toko_details_subtotal'] = $row['return_toko_details_subtotal'];
					$row['return_toko_details_gudang'] = $row['return_toko_details_gudang'];
					$this->Detail->Add($row);
				}
				
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];					
					$return_toko_details_id	= $row['return_toko_details_id'];
					unset($row['return_toko_details_id']);					
					//$row['jumlah'] = (float)$row['jumlah'];					
					$this->Detail->Update($row, array('return_toko_details_id'	=> $return_toko_details_id));
				}
			
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$cabang_kode	= get('cabang_kode');
		
		if ($cabang_kode != "")
		{
			// Delete Single
			$arr_rule	= array('cabang_kode' => $cabang_kode);
			$ls_result = $this->Cabang->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('cabang_kode' => $row['cabang_kode']);
				$ls_result .= $this->Cabang->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>